package com.sdcqjy.property.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.sdcqjy.mylibrary.OtherUtils;
import com.sdcqjy.mylibrary.widget.SpacesItemDecoration;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.SearchAdapter;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.ListMsgMode;
import com.sdcqjy.property.presenter.SearchPresenter;
import com.sdcqjy.property.presenter.contract.SearchContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/22.
 */
public class SearchActivity extends SimpleBaseActivity<SearchPresenter> implements SearchContract.View
        , OnRefreshListener, SearchAdapter.OnAdapterClickListener {
    @BindView(R.id.textTitle)
    EditText textTitle;
    @BindView(R.id.imgClear)
    ImageView imgClear;
    @BindView(R.id.listView)
    RecyclerView listView;
    @BindView(R.id.smartLayout)
    SmartRefreshLayout smartLayout;
    private List<ListMsgMode> modeList;
    private SearchAdapter adapter;

    public static void open(Context context) {
        Intent intent = new Intent(context, SearchActivity.class);
        context.startActivity(intent);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        presenter = new SearchPresenter(this);

        textTitle.addTextChangedListener(new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            public void afterTextChanged(Editable s) {
                String s1 = textTitle.getText().toString();
                if (TextUtils.isEmpty(s1)) {
                    modeList.clear();
                    imgClear.setVisibility(View.INVISIBLE);
                } else imgClear.setVisibility(View.VISIBLE);
            }
        });

        modeList = new ArrayList<>();
        adapter = new SearchAdapter(getActivity(), modeList).setOnAdapterClickListener(this);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        listView.setAdapter(adapter);
        listView.addItemDecoration(new SpacesItemDecoration(OtherUtils.dip2px(getActivity(), 10), new boolean[]{false, false, false, true}));

        smartLayout.setOnRefreshListener(this);
        smartLayout.setEnableLoadmore(false);
    }

    protected void asynRun() {
    }

    public void applyError(String t) {
        smartLayout.finishRefresh();
        super.applyError(t);
    }

    @OnClick(R.id.imgClear)
    public void onImgClearClicked() {
        textTitle.setText("");
    }

    @OnClick(R.id.textSearch)
    public void onTextSearchClicked() {
        String s = textTitle.getText().toString();
        if (TextUtils.isEmpty(s)) return;
        presenter.searchList(s);
    }

    public void searchListRet(List<ListMsgMode> list) {
        smartLayout.finishRefresh();
        if (list == null) list = new ArrayList<>();
        modeList.clear();
        modeList.addAll(list);
        adapter.notifyDataSetChanged();
    }

    public void onRefresh(RefreshLayout refreshlayout) {
        String keyword = presenter.getKeyword();
        textTitle.setText(keyword);
        presenter.searchList(keyword);
    }

    public void onAdapterClicked(ListMsgMode mode) {
        DetailsActivity.open(getActivity(), mode);
    }
}
